﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using ACADApplication = Autodesk.AutoCAD.ApplicationServices.Application;
using ACADDatabase = Autodesk.AutoCAD.DatabaseServices.Database;
using ACADDocument = Autodesk.AutoCAD.ApplicationServices.Document;

namespace YNCNumberSys
{
    public class ZJAppendXData
    {
        #region 属性
        /// <summary>
        /// 实体对象ID.
        /// </summary>
        private ObjectId _id_EntityObjectId;
        /// <summary>
        /// 扩展数据名称.
        /// </summary>
        private string _str_XDataName;
        /// <summary>
        /// 扩展数据.
        /// </summary>
        private ResultBuffer _rbf_XData;
        /// <summary>
        /// 事务,为空表示操作当前图纸空间.
        /// </summary>
        private Transaction _trs_Transaction;
        /// <summary>
        /// 图纸文档WorkingDatabase,为空表示当前的图纸空间.
        /// </summary>
        private ACADDatabase _wdb_DataBase;
        /// <summary>
        /// 该值如果为True,则(1)_trs_Transaction,_wdb_DataBase均为当前图纸空间;(2)事务提交在该类内完成.
        /// </summary>
        private bool _b_TransactionIsUsed;
        #endregion
        public ZJAppendXData(ACADDatabase wdb_DataBase,Transaction trs_Transaction,
            ObjectId id_EntityObjectId, string str_XDataName, ResultBuffer rbf_XData)
        {
            _wdb_DataBase=wdb_DataBase;
            _trs_Transaction = trs_Transaction;
            _id_EntityObjectId = id_EntityObjectId;
            _str_XDataName = str_XDataName;
            _rbf_XData = rbf_XData;
            _b_TransactionIsUsed=true;
        }
        public ZJAppendXData(ObjectId id_EntityObjectId, string str_XDataName,
            ResultBuffer rbf_XData)
        {
            _wdb_DataBase= HostApplicationServices.WorkingDatabase;
            _trs_Transaction = null;
            _id_EntityObjectId = id_EntityObjectId;
            _str_XDataName = str_XDataName;
            _rbf_XData = rbf_XData;
            _b_TransactionIsUsed=false;
        }
        private void DoAppendXData()
        {
            DBObject obj_Entity = _trs_Transaction.GetObject(_id_EntityObjectId, OpenMode.ForWrite);
            RegAppTable acRegAppTbl;
            acRegAppTbl = (RegAppTable)_trs_Transaction.GetObject(_wdb_DataBase.RegAppTableId, OpenMode.ForRead);
            if (acRegAppTbl.Has(_str_XDataName) == false)
            {
                RegAppTableRecord acRegAppTblRec = new RegAppTableRecord();
                acRegAppTblRec.Name = _str_XDataName;
                acRegAppTbl.UpgradeOpen();
                acRegAppTbl.Add(acRegAppTblRec);
                _trs_Transaction.AddNewlyCreatedDBObject(acRegAppTblRec, true);
            }
            obj_Entity.XData = _rbf_XData;
        }
        public void Do()
        {
            if(!_b_TransactionIsUsed)
            {
                using(_trs_Transaction= _wdb_DataBase.TransactionManager.StartTransaction())
                {
                    DoAppendXData();
                    _trs_Transaction.Commit();
                    _trs_Transaction.Dispose();
                }
            }
            else
            {
                DoAppendXData();
            }
        }
    }
}
